﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using DataAccess;
using Entities;

namespace Business
{
    public class DirectorateBusiness
    {
        public static DirectorateBusiness Instance { get; private set; }

        static DirectorateBusiness() { Instance = new DirectorateBusiness(); }

        private Directorate ExtractRawData(IDataRecord dataReader)
        {
            return new Directorate
            {
                DirectorateId = new Guid(dataReader["DirectorateID"].ToString()),
                AddressId = new Guid(dataReader["AddressID"].ToString()),
                AddressLine1 = dataReader["AddressLine1"].ToString(),
                AddressLine2 = dataReader["AddressLine2"].ToString(),
                AddressLine3 = dataReader["AddressLine3"].ToString(),
                ContactName = dataReader["FirstName"] + " " + dataReader["SurName"],
                PostCode = dataReader["PostCode"].ToString(),
                Name = dataReader["Name"].ToString(),
                IsActive = (bool)dataReader["IsActive"]
            };
        }

        public Directorate GetById(Guid directorateId)
        {
            if (directorateId == Guid.Empty)
            {
                return null;
            }
            using (var dataReader = DirectorateDataAccess.Instance.GetById(directorateId))
            {
                if (dataReader.Read())
                {
                    return ExtractRawData(dataReader);
                }
                return null;
            }
        }

        public Collection<Directorate> GetAll(Guid orgId,bool include)
        {
            var colDirectorates = new Collection<Directorate>();
            using (var dataReader = DirectorateDataAccess.Instance.GetAll(orgId,include))
            {
                while (dataReader.Read())
                {
                    colDirectorates.Add(ExtractRawData(dataReader));
                }
                return colDirectorates;
            }
        }
    }
}
